package com.victor.core.util;

import com.victor.common.exception.BizExceptionEnum;
import com.victor.common.exception.BussinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * file 工具类
 */
public class FileUtil {
    private static Logger log = LoggerFactory.getLogger(FileUtil.class);

    /**
     * NIO way
     */
    public static byte[] toByteArray(String filename) {

        File f = new File(filename);
        if (!f.exists()) {
            log.error("文件未找到！" + filename);
            throw new BussinessException(BizExceptionEnum.FILE_NOT_FOUND);
        }
        FileChannel channel = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(f);
            channel = fs.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
                // do nothing
                // System.out.println("reading");
            }
            return byteBuffer.array();
        } catch (IOException e) {
            throw new BussinessException(BizExceptionEnum.FILE_READING_ERROR);
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                throw new BussinessException(BizExceptionEnum.FILE_READING_ERROR);
            }
            try {
                fs.close();
            } catch (IOException e) {
                throw new BussinessException(BizExceptionEnum.FILE_READING_ERROR);
            }
        }
    }

    /**
     * 读取文件 最后操作时间
     *
     * @Title:
     * @Description:
     * @author: Jiabin.Zhao
     * @CreationTime: 2017年8月24日 下午7:57:58
     */
    public static Long getFileLastModified(String path) {
        File file = new File(path);
        return getFileLastModified(file);
    }

    /**
     * 读取文件 最后操作时间
     *
     * @Title:
     * @Description:
     * @author: Jiabin.Zhao
     * @CreationTime: 2017年8月24日 下午7:57:58
     */
    public static Long getFileLastModified(File file) {
        return file.lastModified();
    }

    /**
     * @Auther:Victor
     * @Description:获取文件 后缀
     * @Date:Create in 11:57 2018/4/21
     * @Modified By:
     */
    public static String getSuffix(String fileName) {
        if (fileName != null && fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf("."));
        }
        return null;
    }

    public static String moveFile(File file, String newFileName, String fileRoot) {
        if (file == null)
            return null;

        if (!file.exists()) {
            return null;
        }
        File newfile = new File(newFileName);

        if (!newfile.getParentFile().exists()) {
            newfile.getParentFile().mkdirs();
        }

        file.renameTo(newfile);

        return removePrefix(newFileName, fileRoot);
    }

    public static String removePrefix(String src, String prefix) {
        if (src != null && src.startsWith(prefix)) {
            return src.substring(prefix.length());
        }
        return src;
    }
}